package com.angel.common.converters.base;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.collections15.Transformer;

import com.angel.common.converters.transformers.BooleanTransformer;
import com.angel.common.converters.transformers.CharacterTransformer;
import com.angel.common.converters.transformers.DoubleTransformer;
import com.angel.common.converters.transformers.FloatTransformer;
import com.angel.common.converters.transformers.IntegerTransformer;
import com.angel.common.converters.transformers.LongTransformer;
import com.angel.common.converters.transformers.ShortTransformer;
import com.angel.common.converters.transformers.StringTransformer;


/**
 *
 * @author William
 *
 */
public class Converter {
	private static BooleanTransformer BOOLEAN_TRANSFORMER = new BooleanTransformer();
	private static CharacterTransformer CHARACTER_TRANSFORMER = new CharacterTransformer();
	private static DoubleTransformer DOUBLE_TRANSFORMER = new DoubleTransformer();
	private static FloatTransformer FLOAT_TRANSFORMER = new FloatTransformer();
	private static IntegerTransformer INTEGER_TRANSFORMER = new IntegerTransformer();
	private static LongTransformer LONG_TRANSFORMER = new LongTransformer();
	private static ShortTransformer SHORT_TRANSFORMER = new ShortTransformer();
	private static StringTransformer STRING_TRANSFORMER = new StringTransformer();

	private static Map<Class<?>,ObjectTransformer> TRANSFORMERS = new HashMap<Class<?>, ObjectTransformer>();

	static {
		TRANSFORMERS.put(Boolean.class, new BooleanTransformer());
		TRANSFORMERS.put(Character.class, new CharacterTransformer());
		TRANSFORMERS.put(Double.class, new DoubleTransformer());
		TRANSFORMERS.put(Float.class, new FloatTransformer());
		TRANSFORMERS.put(Integer.class, new IntegerTransformer());
		TRANSFORMERS.put(Long.class, new LongTransformer());
		TRANSFORMERS.put(Short.class, new ShortTransformer());
		TRANSFORMERS.put(String.class, new StringTransformer());

	}

	private Converter(){
		super();
	}

	@SuppressWarnings("unchecked")
	public static Object convert(Class<?> primitiveClass, Object object, Class<? extends Object> convertTo){
		ObjectTransformer objectTransformer = TRANSFORMERS.get(primitiveClass);
		Transformer<Object, Object> transformerImpl = (Transformer<Object, Object>) objectTransformer.getTransformer(convertTo);
		return transformerImpl.transform(object);
	}

	public static Object convertTo(Boolean object, Class<? extends Object> converterTo){
		Transformer<Boolean, ? extends Object> c = BOOLEAN_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Character object, Class<? extends Object> converterTo){
		Transformer<Character, ? extends Object> c = CHARACTER_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Double object, Class<? extends Object> converterTo){
		Transformer<Double, ? extends Object> c = DOUBLE_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Float object, Class<? extends Object> converterTo){
		Transformer<Float, ? extends Object> c = FLOAT_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Integer object, Class<? extends Object> converterTo){
		Transformer<Integer, ? extends Object> c = INTEGER_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Long object, Class<? extends Object> converterTo){
		Transformer<Long, ? extends Object> c = LONG_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(Short object, Class<? extends Object> converterTo){
		Transformer<Short, ? extends Object> c = SHORT_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}

	public static Object convertTo(String object, Class<? extends Object> converterTo){
		Transformer<String, ? extends Object> c = STRING_TRANSFORMER.getTransformer(converterTo);
		return  c.transform(object) ;
	}
}
